﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using SqlSugar;
using CashFlow.Db.DAL;
using CashFlow.Db.Entity;
using CashFlow.ShopManage.Def;
using CashFlow.ShopManage.Common;

namespace CashFlow.ShopManage.Service.Implement
{
    public class ShopService : Service<ShopInfoEntity>, IShopService
    {
        public IList<ShopInfoEntity> QueryNotSelectedShopList()
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls = db.Queryable<ShopInfoEntity>()
                            .Where(x => SqlFunc.Subqueryable<AdminEntity>()
                                    .Where(y => y.ShopManagerUserId == x.ShopManagerUserId)
                                    .NotAny())
                            .ToList();
                return ls;
            }
        }

        public ShopInfoEntity QueryShopInfoByReferenceInvitationCode(
            string invitationCode)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var entity = db.Queryable<ShopInfoEntity>()
                               .InnerJoin<ShopUserEntity>((si, su) => si.ShopManagerUserId == su.ShopManagerUserId)
                               .InnerJoin<UserInfoEntity>((si, su, u) => su.UserId == u.Id)
                               .Where((si, su, u) => u.InvitationCode == invitationCode)
                               .Select((si, su, u) => si)
                               .First();
                return entity;
            }
        }

        public ShopInfoEntity QueryShopInfoByUserId(int userId)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var entity = db.Queryable<ShopInfoEntity>()
                               .InnerJoin<ShopUserEntity>((si, su) => si.ShopManagerUserId == su.ShopManagerUserId)
                               .Where((si, su) => su.UserId == userId)
                               .Select((si, su) => si)
                               .First();
                return entity;
            }
        }
    }
}